<?php

class Snoopy{

	var $port			=	80;
	var $host			=	"";
	var	$maxlength		=	1000;
	var $_httpmethod	=	"GET";
	var $_httpversion	=	"HTTP/1.0";
	var $agent			=	"qxiang v1.0";
	var $accept			=	"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
	var $results		=	"";
	var $user			=	"";
	var $pass			=	"";
	var $timeout		=	20;

public function _connect(&$fp){

	$host=$this->host;

	$port=$this->port;

	if($fp = fsockopen(
					$host,
					$port,
					$errno,
					$errstr,
					$this->timeout
					))
		{			
			return true;

		}else return false;

}
	
public function _disconnect($fp){
	return(fclose($fp));
}

public function remoteRequest($url,$fp,$URI,$http_method){

		$URI_PARTS = parse_url($URI);
		if(empty($url))
			$url = "/";
		$headers = $http_method." ".$url." ".$this->_httpversion."\r\n";
		
		if(!empty($this->host)) {
			$headers .= "Host: ".$this->host;
			if(!empty($this->port))
				$headers .= ":".$this->port;
			$headers .= "\r\n";
		}
	
		$headers .= "Connection: Close\r\n\r\n";

		fwrite($fp, $headers);
		while($currentHeader = fgets($fp,128)){
			if($currentHeader == "\r\n")
				break;
			$heads[]=$currentHeader;

		}

		$this->results='';
		do{

			$_data=fread($fp,500);
		
			if(strlen($_data)==0){
				file_put_contents("ser.txt",$this->results);
				break;
			}
			$this->results.=$_data;
		}while(true);

		return true;

}

public function fetch($uri){

	$uriPart=parse_url($uri);

	if (!empty($uriPart["user"]))
		$this->user = $uriPart["user"];
				
	if (!empty($uriPart["pass"]))
		$this->pass = $uriPart["pass"];

	if (empty($uriPart["query"]))
		$uriPart["query"] = '';
				
	if (empty($uriPart["path"]))
		$uriPart["path"] = '';

	$this->host = $uriPart["host"];
				
	if(!empty($uriPart["port"]))
		$this->port = $uriPart["port"];

	if($this->_connect($fp)){

		$path = $uriPart["path"].($uriPart["query"] ? "?".$uriPart["query"] : "");

		$this->remoteRequest($path, $fp, $URI, $this->_httpmethod);

	}

	$this->_disconnect($fp);

}
}

?>
